package LinkedList;

public class MySingleLinkedList {

    //节点定义为内部类
    class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//代表链表的头结点

    //穷举法创建链表(事实上并不是这么创建的)
    public void createList() {
        ListNode node1=new ListNode(12);
        ListNode node2=new ListNode(23);
        ListNode node3=new ListNode(34);
        ListNode node4=new ListNode(45);
        ListNode node5=new ListNode(56);

        head=node1;
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        node5.next=null;
    }

    public void display() {
        ListNode node=head;
        while(node!=null) {
            System.out.print(node.val+" ");
            node=node.next;
        }
        System.out.println();
    }

    public int size() {
        ListNode node=head;
        int count=0;
        while(node!=null) {
            count++;
            node=node.next;
        }
        return count;
    }

    public void addAtHead(int val) {
        ListNode node=new ListNode(val);
        node.next=head;
        head=node;
    }

    public void addAtTail(int val) {
        ListNode node=new ListNode(val);
        ListNode cur=head;
        if(head==null) {    //若head为空,不能访问head.next
            head=node;
            return;
        }
        while(cur.next!=null) {
            cur=cur.next;
        }
        cur.next=node;
    }

    public void addAtIndex(int index,int val) {
        //判断index的合法性
        try {
            checkPos(index);
        }catch(IndexNotLegalException e) {
            e.printStackTrace();
        }
        //头插尾插
        if(index==0) {
            addAtHead(val);
            return;
        }
        if(index==size()-1) {
            addAtTail(val);
            return;
        }
        //找到cur的前一个节点
        ListNode node=new ListNode(val);
        ListNode cur=head;
        int count=0;
        while(count!=index-1){    //cur要走index-1步,才能走到cur前面的节点
            cur=cur.next;
            count++;
        }
        //连接
        node.next=cur.next;
        cur.next=node;
    }
    private void checkPos(int pos) throws IndexNotLegalException{
        if(pos<0 || pos>size()) {
            throw new IndexNotLegalException("Index不合法");
        }
    }

    public boolean contains(int val) {
        ListNode cur=head;
        while(cur!=null) {
            if(cur.val==val) return true;
            cur=cur.next;
        }
        return false;
    }

    //删除第一次出现的val
    public void remove(int val) {
        if(head==null) return;
        if(head.val==val) {
            head=head.next;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null) {
            if(cur.next.val==val) {
                cur.next=cur.next.next;
                return;
            }
            cur=cur.next;
        }
    }

    //删除所有val
    public void removeAllKey(int val) {
        //1.判空
        if(head==null) return;
        //2.定义prev和cur
        ListNode prev=head;
        ListNode cur=head.next;
        //3.开始判断并删除
        while(cur!=null) {
            if(cur.val==val) {
                prev.next=cur.next;
            } else {
                prev=cur;
            }
            cur=cur.next;
        }
        //4.处理头结点
        if(head.val==val) {
            head=head.next;
        }
    }

    public void clear() {
//        head=null;//最直接的方式
        ListNode cur=head;
        while(cur!=null) {
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;
    }

}
